package com.example.demo.redis.client;

import com.example.demo.redis.able.CacheInterface;
import com.example.demo.redis.util.RedissonUtils;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.codec.TypedJsonJacksonCodec;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
public class RedissonMapClient {

    @Autowired
    private RedissonClient redissonClient;


/**
     * 获取RMap
     */

    public <K, V> RMap<K, V> getMap(CacheInterface cacheInterface, Codec codec) {
        String cacheKey = RedissonUtils.getCacheKey(cacheInterface, null);
        return redissonClient.getMap(cacheKey, codec);
    }


/**
     * 获取RMap
     */

    public RMap<String, String> getMap(CacheInterface cacheInterface, String key) {
        return getMap(cacheInterface, key, String.class, String.class);
    }


/**
     * 获取RMap
     */

    public <V> RMap<String, V> getMap(CacheInterface cacheInterface, Class<?> mapValueClass) {
        return getMap(cacheInterface, "", mapValueClass);
    }


/**
     * 获取RMap
     */

    public <V> RMap<String, V> getMap(CacheInterface cacheInterface, String key, Class<?> mapValueClass) {
        return getMap(cacheInterface, key, String.class, mapValueClass);
    }


/**
     * 获取RMap
     */

    public <K, V> RMap<K, V> getMap(CacheInterface cacheInterface, Class<?> mapKeyClass, Class<?> mapValueClass) {
        return getMap(cacheInterface, null, mapKeyClass, mapValueClass);
    }


/**
     * 获取RMap
     */

    public <K, V> RMap<K, V> getMap(CacheInterface cacheInterface, String key, Class<?> mapKeyClass, Class<?> mapValueClass) {
        String cacheKey = RedissonUtils.getCacheKey(cacheInterface, key);
        Codec codec = new TypedJsonJacksonCodec(mapKeyClass, mapValueClass);
        return redissonClient.getMap(cacheKey, codec);
    }

}

